<template>
  <div>
    <el-tag
      v-for="(o,i) in value"
      :key="i"
      :type="orgTypeColorMap[o.orgType]"
      closable
      class="inline-block m5"
      @close="value.splice(i, 1)"
    >{{ o.orgName }}</el-tag>
    <el-divider v-if="value.length > 0" direction="vertical" />
    <el-button size="small" icon="el-icon-plus" @click="showOrgTag=true">选择</el-button>
    <el-button v-show="value.length > 1" size="small" icon="el-icon-close" @click="value = []">清空</el-button>
    <el-dialog
      title="选择组织结构"
      :visible.sync="showOrgTag"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="800px"
      destroy-on-close
      @open="openTag"
    >
      <component :is="orgTag" ref="tag" :multi="true" />
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="addTag">确 定</el-button>
        <el-button size="small" @click="closeTagWin">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Mixin } from '@/utils/mixin'
export default {
  name: 'FullOrgView',
  mixins: [Mixin],
  components: {},
  props: {
    value: {
      type: Array,
      default: ()=> []
    },
    mode: {
      type: String,
      default: 'full-tag'
    }
  },
  data() {
    return {
      showOrgTag: false,
    }
  },
  computed: {
    orgTag() {
      return require(`/src/components/tag/${this.mode}`).default
    },
  },
  mounted() {

  },
  methods: {
    addTag() {
      const p = this.$refs.tag.get()
      if (p.length === 0) {
        this.$message.warning('请选择组织结构')
        return
      }

      // 去重
      const ids = []
      let parent = this.value
      // debugger
      for (let i = 0; i < parent.length; i++) {
        ids.push(parent[i].orgId)
      }
      for (let i = 0; i < p.length; i++) {
        if (ids.indexOf(p[i].orgId) === -1) {
          parent.push(p[i])
          ids.push(p[i].orgId)
        }
      }
      this.$emit('addTag',parent,ids)
      this.showOrgTag = false
    },
    openTag() {
      this.$emit('openTagWin')
      this.$nextTick(() => {
        this.$refs.tag.query()
      })
    },
    closeTagWin() {
      this.showOrgTag = false
      this.$emit('closeTagWin')
    },
    clear() {
      //this.value = []
    }
  }
}
</script>
<style scoped>

</style>
